package com.zcah.db.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcah.common.util.PageUtils;
import com.zcah.common.vo.Query;
import com.zcah.common.vo.R;
import com.zcah.db.domain.DbsourceDO;
import com.zcah.db.service.DbsourceService;

/**
 * 脱敏数据库源连接信息
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-07-06 13:59:44
 */

@RestController
@RequestMapping("/v1/dbsource")
public class DbsourceController {

	private final DbsourceService dbsourceService;

	@Autowired
	public DbsourceController(DbsourceService dbsourceService) {
		this.dbsourceService = dbsourceService;
	}

	@GetMapping("/list")
	@RequiresAuthentication
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<DbsourceDO> dbsourceList = dbsourceService.list(query);
		int total = dbsourceService.count(query);
		PageUtils pageUtils = new PageUtils(dbsourceList, total);
		return pageUtils;
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresAuthentication
	public R save(@RequestBody DbsourceDO dbsource) {
		dbsource.setCreateTime(new Date());
		if (dbsourceService.save(dbsource) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@RequiresAuthentication
	public R update(@RequestBody DbsourceDO dbsource) {
		dbsource.setCreateTime(new Date());
		dbsourceService.update(dbsource);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove/{id}")
	@RequiresAuthentication
	public R remove(@PathVariable Integer id) {
		if (dbsourceService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@RequiresAuthentication
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		dbsourceService.batchRemove(ids);
		return R.ok();
	}

}
